<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="../../js/axios.js"></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../js/api.js"></script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>品牌管理</li>
    <li>修改</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>品牌信息</span></div>

  <form id="form">
    <ul class="forminfo">
      <li>
        <label>品牌名称</label>
        <input name="brandName" type="text" class="dfinput" id="brandName"/>
        <i class="tips">必填，不能超过50个字符</i>
      </li>
      <li>
        <label>状态</label>
        <select class="dfselect" name="brandStatus">
          <option value="1">可用</option>
          <option value="2">不可用</option>
        </select>
        <i>必选</i>
      </li>
      <li>
        <label>创建人</label>
        <input name="createId" type="hidden" id="createId"/>
        <input name="createName" type="text" id="createName" readonly="readonly" class="roinput" />
        <i>不能编辑</i>
      </li>
      <li>
        <label>创建时间</label> <!--value="2017-11-18 15:36:10" readonly="readonly"-->
        <input name="createTime" id="createTime" class="roinput" readonly="readonly"/>
        <i>不能编辑</i>
      </li>
      <li>
        <label>&nbsp;</label>
        <input name="" type="submit" class="btn" value="确定"/>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="" type="button" class="btn" value="返回" onclick="window.location.href='brandList.html'"/>
      </li>
    </ul>
  </form>
</div>
<script type="text/javascript">
  $(function () {

    let brandList = [];
    axiosJson(
            'get',//提交方法
            '/purchase/brandName',//提交地址
            null,
            null
    ).then((res) => {
      brandList = res.data.resultData
    })


    //获取id
    let brandId=getParameter("id");
    let name='';
    echo();   //数据回显
    $('#brandName').blur(check);  //光标验证


    $('#form').submit(function () {
      check();
      return false;
    })

    // 数据回显
    function echo() {
      axiosJson(
              'get',//提交方法
              '/purchase/brandPage',//提交地址
              null,
              {brandId:brandId}
      ).then((res) => {
        let audit = res.data.resultData.data[0];
        $('#brandName').val(audit.brandName);
        $('#brandStatus').val(audit.brandStatus);
        $('#createId').val(audit.createId);
        $('#createName').val(audit.creator.uName);
        $('#createTime').val(audit.createTime);
        name=audit.brandName;
      })

      //表单提交
      $('#form').submit(function () {
        let brand = $('#form').serializeArray();
        if (check()){
          let flag=confirm("您确定修改吗")
          if (flag){
            axiosJson(
                    'get',//提交方法
                    '/purchase/alterBrand',//提交地址
                    brand,
                    {brandId:brandId}
            ).then((res) => {
              location.href="brandList.html";
            })
          }
          return false;
        }
      })
    }

    //表单验证  品牌非空与是否存在验证
    function check() {
      // $("#form").unbind("submit")
      // $('#form').submit(function () {
      //   check();
      //   return false;
      // })
      $('.tips').html("");
      var isSuccess=true;
      let brandName=$('#brandName').val();
      if (brandName==null||brandName==''){
        $('.tips').html("品牌名称不能为空").css("color","red");
        isSuccess=false;
      }else {
        for (let i = 0; i < brandList.length; i++) {
          if (brandName==brandList[i]&&brandName!=name){
            $('.tips').html("该品牌已存在").css("color","red");
            isSuccess=false;
            break;
          }
        }
      }
      return isSuccess;
    }

  })

</script>
</body>
</html>
